/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */
package org.isqlviewer.xml;

import java.net.URL;
import java.util.Hashtable;

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * @author mkobold
 */
public class SaxResolver implements EntityResolver {

    /**
     * The URLs of dtds and schemas that have been registered, keyed by the public identifier that corresponds.
     */
    protected Hashtable<String, String> entityValidator = new Hashtable<String, String>();

    /**
     * Extension to make the difference between DTD and Schema.
     */
    protected String schemaExtension = "xsd";

    /**
     * Create a new <code>EntityResolver</code> that will redirect all remote dtds and schema to a locat destination.
     * 
     * @param digester The digester instance.
     */
    public SaxResolver() {

    }

    /**
     * Register the specified DTD/Schema URL for the specified public identifier. This must be called before the first
     * call to <code>parse()</code>. When adding a schema file (*.xsd), only the name of the file will get added. If
     * two schemas with the same name are added, only the last one will be stored.
     * 
     * @param publicId Public identifier of the DTD to be resolved
     * @param entityURL The URL to use for reading this DTD
     */
    public void register(String publicId, String entityURL) {

        String key = publicId;
        if (publicId.indexOf(schemaExtension) != -1)
            key = publicId.substring(publicId.lastIndexOf('/') + 1);
        entityValidator.put(key, entityURL);
    }

    public void register(String publicId, String entityURL, Class< ? > clazz) {

        URL url = clazz.getResource(entityURL);
        register(publicId, url.toString());
    }

    /**
     * Resolve the requested external entity.
     * 
     * @param publicId The public identifier of the entity being referenced
     * @param systemId The system identifier of the entity being referenced
     * @exception SAXException if a parsing exception occurs
     */
    public InputSource resolveEntity(String publicId, String systemId) throws SAXException {

        // Has this system identifier been registered?
        String entityURL = null;
        if (publicId != null) {
            entityURL = entityValidator.get(publicId);
        }

        // Redirect the schema location to a local destination
        String key = null;
        if (entityURL == null && systemId != null) {
            key = systemId.substring(systemId.lastIndexOf('/') + 1);
            entityURL = entityValidator.get(key);
        }

        if (entityURL == null) {
            return (null);
        }

        try {
            return (new InputSource(entityURL));
        } catch (Exception e) {
            throw new SAXException(e);
        }
    }
}
